package net.oschina.app.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.adapter.MainLeftAdapter;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.constants.MainTab;
import net.oschina.app.ui.view.MyDialog;
import net.oschina.app.ui.view.MyTabHost;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    String[] bottom_tabs = new String[]{"综合", "动弹", "加号", "发现", "我"};
    int[] bottom_icons = new int[]{R.drawable.tab_icon_new, R.drawable.tab_icon_tweet, R.drawable.tab_icon_explore, R.drawable.tab_icon_me};
    @BindView(R.id.flt_content)
    FrameLayout mFltContent;
    @BindView(R.id.tab_host)
    MyTabHost mTabHost;
    @BindView(R.id.iv_quick_option)
    ImageView mIvQuickOption;
    @BindView(R.id.ll_content)
    LinearLayout mLlContent;
    @BindView(R.id.lv_main_left)
    ListView mLvMainLeft;
    @BindView(R.id.iv_setting)
    ImageView mIvSetting;
    @BindView(R.id.rl_left_setting)
    RelativeLayout mRlLeftSetting;
    @BindView(R.id.iv_exit)
    ImageView mIvExit;
    @BindView(R.id.rl_left_exit)
    RelativeLayout mRlLeftExit;
    @BindView(R.id.drawerLayout)
    DrawerLayout mDrawerLayout;
    public MainLeftAdapter mLeftAdapter;
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initBottom();
        initLeft();
    }

    private void initLeft() {
        ListView lvMainLeft = (ListView) findViewById(R.id.lv_main_left);
        mLeftAdapter = new MainLeftAdapter(this);
        lvMainLeft.setAdapter(mLeftAdapter);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        lvMainLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
//                        Toast.makeText(MainActivity.this, "技术问答", Toast.LENGTH_SHORT).show();
                        Intent intent1 = new Intent(MainActivity.this, TechnologyAndAnswerActivity.class);
                        startActivity(intent1);
                        break;
                    case 1:
//                        Toast.makeText(MainActivity.this, "开源中国", Toast.LENGTH_SHORT).show();
                        Intent intent2 = new Intent(MainActivity.this, OpenSoftwareActivity.class);
                        startActivity(intent2);
                        break;
                    case 2:
//                        Toast.makeText(MainActivity.this, "博客区", Toast.LENGTH_SHORT).show();
                        Intent intent3 = new Intent(MainActivity.this, OpenSoftwareActivity.class);
                        startActivity(intent3);
                        break;
                    case 3:
//                        Toast.makeText(MainActivity.this, "Git客户端", Toast.LENGTH_SHORT).show();
                        Intent intent4 = new Intent();
                        intent4.setAction(Intent.ACTION_VIEW);
                        intent4.setData(Uri.parse("http://git.oschina.net/appclient"));
                        startActivity(intent4);
                        break;
                }
            }
        });
    }

    private void initBottom() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        mTabHost.setup(getApplicationContext(), fragmentManager, R.id.flt_content);
        initTab();

    }

    public void initTab() {
        MainTab[] values = MainTab.values();
        for (int i = 0; i < values.length; i++) {
            MainTab mainTab = values[i];
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(this.getResources().getString(mainTab.getResName()));
            View indicator = View.inflate(getApplicationContext(), R.layout.tab_indicator, null);
            tabSpec.setIndicator(indicator);

            Drawable drawable = this.getResources().getDrawable(mainTab.getResIcon());
            TextView tvTitle = (TextView) indicator.findViewById(R.id.tab_title);
            if (i == 2) {
                indicator.setVisibility(View.INVISIBLE);
                tvTitle.setBackground(drawable);
            }
            tvTitle.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            tvTitle.setText(mainTab.getResName());
            Bundle args = new Bundle();
            args.putString("key", getString(mainTab.getResName()));
            mTabHost.addTab(tabSpec, mainTab.getClz(), args);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_activity_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                Toast.makeText(this, "开始搜索", Toast.LENGTH_SHORT).show();
                return true;
            case android.R.id.home:
                drawerToggle();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void drawerToggle() {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawer(Gravity.LEFT);
        } else {
            mDrawerLayout.openDrawer(Gravity.LEFT);
        }
    }



    @OnClick({R.id.iv_quick_option, R.id.rl_left_setting, R.id.rl_left_exit, R.id.drawerLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_quick_option: //加号
                showQuickDialog();//点击加号后弹出对话框
                break;
            case R.id.rl_left_setting://侧滑之设置
                enterSetting();//设置界面的跳转
                break;
            case R.id.rl_left_exit: //侧滑之离开
                break;
            case R.id.drawerLayout:
                break;
        }
    }

    /***********************
     * 点击加号弹出自定义对话框start
     ***********************************************/
    //点击加号后弹出对话框
    private void showQuickDialog() {

        final MyDialog dialog = new MyDialog(this);
        dialog.show();
        dialog.getWindow().setWindowAnimations(R.style.AnimBottom); //  dialog由下往上出现的动画
        dialogListening(dialog);   //  设置dialog点击文字,相片,拍照,关闭的事件


    }

    /**
     * 设置dialog点击文字,相片,拍照,关闭的回调
     *
     * @param dialog
     */
    private void dialogListening(final MyDialog dialog) {
        dialog.setOnClickItemListening(new MyDialog.OnClickItemListening() {
            @Override
            public void clickWrite() {
//                Toast.makeText(MainActivity.this, "文字", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), TweetPubActivity.class);
                startActivity(intent);
                dialog.dismiss();
            }

            @Override
            public void clickPhoto() {
                Toast.makeText(MainActivity.this, "相片", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void clickTackPicture() {
                Toast.makeText(MainActivity.this, "拍照", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void clickClose() {
                dialog.cancel();     //  关闭自定义dialog
            }
        });
    }



    /*//ViewPager与按钮关联方法
    private void onPagerSelected(View menuButton,int position){
        //如果已经被选中了就return掉
        if(menuButton.isSelected()){
            return;
        }
        mViewPager.setCurrentItem(position, false);  //  取消ViewPager切换动画
        currentSelectedMenu.setSelected(false);
        switch (position){
            case 0:
                currentSelectedMenu =mMenuNews;
                break;
            case 1:
                currentSelectedMenu =mMenuTweet;
                break;
            case 2:
                currentSelectedMenu =mMenuFind;
                break;
            case 3:
                currentSelectedMenu =mMenuMe;
                break;
        }
        currentSelectedMenu.setSelected(true);
    }*/

    /***********************
     * 点击加号弹出自定义对话框end
     ***********************************************/
    //设置界面的跳转
    private void enterSetting() {
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }
}
